// pages/updateInfo/updateInfo.js
const API = require('../../utils/api.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEditing: false,
    usertype: "",
    username: "",
    email: "",
    tel: "",
    department: "",
    position: "",
    joinDate: "",
    avatarUrl: "",
    creditScore: "98",
    totalPrint: "1248",
    userRole: "高级管理员"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadUserInfo();
  },

  /**
   * 加载用户信息
   */
  loadUserInfo: function() {
    if(app.globalData.isLogin) {
      const userInfo = app.globalData.userinfo || {};
      
      this.setData({
        usertype: app.globalData.usertype,
        username: userInfo.name || "Admin0001",
        email: userInfo.email || "admin@hongtu.com",
        tel: userInfo.tel || "138****8888",
        department: userInfo.department || "技术部",
        position: userInfo.position || "高级管理员",
        joinDate: userInfo.joinDate || "2023-01-15",
        avatarUrl: userInfo.avatarUrl || "/images/person_selected.png"
      });
    }
  },

  /**
   * 切换编辑模式
   */
  changeAvatar: function() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        const tempFilePath = res.tempFilePaths[0];
        // 这里可以上传图片到服务器，然后获取URL
        // 暂时只是显示本地图片
        that.setData({
          avatarUrl: tempFilePath
        });
        
        wx.showToast({
          title: '头像修改成功',
          icon: 'success'
        });
      }
    });
  },
  
  /**
   * 进入编辑模式
   */
  saveChanges: function() {
    if (!this.data.isEditing) {
      this.setData({
        isEditing: true
      });
      return;
    }
    
    // 保存编辑的信息
    const that = this;
    wx.showLoading({
      title: '保存中...',
    });
    
    // 模拟API请求
    setTimeout(function() {
      wx.hideLoading();
      that.setData({
        isEditing: false
      });
      
      wx.showToast({
        title: '信息更新成功',
        icon: 'success'
      });
      
      // 更新本地存储
      if (app.globalData.userinfo) {
        app.globalData.userinfo.name = that.data.username;
        app.globalData.userinfo.email = that.data.email;
        app.globalData.userinfo.tel = that.data.tel;
        app.globalData.userinfo.department = that.data.department;
        app.globalData.userinfo.position = that.data.position;
        app.globalData.userinfo.avatarUrl = that.data.avatarUrl;
        wx.setStorageSync('userinfo', app.globalData.userinfo);
      }
    }, 1000);
  },
  
  /**
   * 取消编辑
   */
  cancelEdit: function() {
    if (this.data.isEditing) {
      this.loadUserInfo(); // 重新加载用户信息，取消编辑
      this.setData({
        isEditing: false
      });
    }
  },
  
  /**
   * 复制邮箱地址
   */
  copyEmail: function() {
    wx.setClipboardData({
      data: this.data.email,
      success: function() {
        wx.showToast({
          title: '邮箱已复制',
          icon: 'success'
        });
      }
    });
  },
  
  /**
   * 复制手机号码
   */
  copyPhone: function() {
    wx.setClipboardData({
      data: this.data.tel,
      success: function() {
        wx.showToast({
          title: '手机号已复制',
          icon: 'success'
        });
      }
    });
  },
  
  /**
   * 处理输入更改
   */
  handleInput: function(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    
    // 使用对象，动态设置属性
    const data = {};
    data[field] = value;
    
    this.setData(data);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 每次显示页面时刷新用户信息
    this.loadUserInfo();
  },
  
  /**
   * 获取API端点
   */
  getUpdateApi: function(type, field) {
    const apiMap = {
      name: {
        customer: API.Customer.updateName,
        seller: API.Seller.updateName,
        producer: API.Producer.updateName,
        transporter: API.Transporter.updateName
      },
      tel: {
        customer: API.Customer.updateTel,
        seller: API.Seller.updateTel,
        producer: API.Producer.updateTel,
        transporter: API.Transporter.updateTel
      },
      location: {
        customer: API.Customer.updateLocation,
        seller: API.Seller.updateLocation,
        producer: API.Producer.updateLocation,
        transporter: API.Transporter.updateLocation
      }
    };
    
    return apiMap[field] && apiMap[field][type] ? apiMap[field][type] : '';
  }
})